package com.regtwo.admin.mplayer;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SeekBar;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    private ListView lv;
    private String[] strs= {"first","second","third"};
    private Button start,prev,next,pause,exit;
    private SeekBar sb ;
    private TextView tv1,tv2;
    MusicService.MyBinder mm;
    static Handler handler;
    Intent intent;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        start = (Button)findViewById(R.id.start);
        prev = (Button)findViewById(R.id.prev);
        next = (Button)findViewById(R.id.next);
        pause = (Button)findViewById(R.id.pause);
        exit = (Button)findViewById(R.id.exit);
        sb = (SeekBar)findViewById(R.id.sb);
        tv1= (TextView)findViewById(R.id.tv1);
        tv2= (TextView)findViewById(R.id.tv2);
        intent = new Intent("musicplayer");



        lv = (ListView)findViewById(R.id.lv);
        lv.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, strs));
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                intent.putExtra("action", "start" + position);
                Log.i("gtq-1", "actionIntent" + "start" + position);
                startService(intent);

                bindService(intent, new ServiceConnection() {
                    @Override
                    public void onServiceConnected(ComponentName name, IBinder service) {
                        mm = (MusicService.MyBinder) service;
                        sb.setMax(mm.getSongLong());
                        tv2.setText(toTime(mm.getSongLong()));
                        MusicService.MusicThread.startThread();
                        handler = new Handler(){
                            @Override
                            public void handleMessage(Message msg) {
                                int process = msg.getData().getInt("position");
                                tv1.setText(toTime(process));
                                sb.setProgress(process);
                            }
                        };
                       // startSeekBarUp();
                    }

                    @Override
                    public void onServiceDisconnected(ComponentName name) {

                    }
                }, BIND_AUTO_CREATE);
            }
        });

        start.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                //Intent intent = new Intent("musicplayer");
                intent.putExtra("action", "start");
                sb.setMax(mm.getSongLong());
                tv2.setText(toTime(mm.getSongLong()));

                startService(intent);
            }
        });
        next.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //Intent intent = new Intent("musicplayer");
                intent.putExtra("action","next");
                sb.setMax(mm.getSongLong());
                tv2.setText(toTime(mm.getSongLong()));
                mm.set_is_paused(0);
                startService(intent);
            }
        });
        prev.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //Intent intent = new Intent("musicplayer");
                intent.putExtra("action","prev");
                sb.setMax(mm.getSongLong());
                tv2.setText(toTime(mm.getSongLong()));
                mm.set_is_paused(0);
                startService(intent);
            }
        });
        pause.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               // Intent intent = new Intent("musicplayer");
                intent.putExtra("action", "pause");
                sb.setMax(mm.getSongLong());
                tv2.setText(toTime(mm.getSongLong()));
                mm.set_is_paused(1);
                startService(intent);
            }
        });
        sb.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                if (fromUser == true) {
                    mm.getPlayer().seekTo(progress);
                    tv1.setText(toTime(progress));
                }
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
        exit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mm.getPlayer().stop();
                android.os.Process.killProcess(android.os.Process.myPid());
            }
        });

    }

    /*Handler handler = new Handler();
    public void startSeekBarUp(){
        handler.post(r);
    }

    Runnable r = new Runnable() {
        @Override
        public void run() {
            int currentPosition = mm.getCurrentProcess();
            Log.i("gtq-1","run currentPosition...."+currentPosition);
            tv1.setText(toTime(currentPosition));
            sb.setProgress(currentPosition);
            handler.postDelayed(r,300);
        }
    };*/



    public String toTime(int mediatime){
        Integer min = mediatime/1000/60;
        Integer s = (mediatime- min*1000*60)/1000;
       //Integer ms = mediatime - min*1000*60-s*1000;
        String minstr = (min.toString()).length()>1?min.toString():"0"+min;
        String sstr = (s.toString()).length()>1?s.toString():"0"+s;
        return minstr+":"+sstr;
    }

    /*public Handler getHandler(){
        return handler;
    }*/
}
